package com.polytech.ihm;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.polytech.business.ConnexionManager;
import com.polytech.ihm.factory.ButtonsPanel;
import com.polytech.ihm.factory.Form;
import com.polytech.utils.BusinessFactory;

public class ConnexionFrame extends JFrame {

	private JPanel mainPanel=new JPanel(new BorderLayout());
	private Form  form=new Form();
	private ButtonsPanel buttonsPanel=new ButtonsPanel(new String[]{"Annuler","Inscritpion","Se connecter"});
	public ConnexionFrame() {
		super("Connexion");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		setContentPane(mainPanel);
		form.addField("Login",15);
		form.addPasswordField("Password","",15);
		mainPanel.add(form,BorderLayout.CENTER);
		mainPanel.add(buttonsPanel,BorderLayout.SOUTH);
		pack();
		setVisible(true);
		buttonsPanel.addActionListner("Annuler",new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		buttonsPanel.addActionListner("Inscritpion",new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
					new InscriptionFrame();
			}
		});
		buttonsPanel.addActionListner("Se connecter",new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ConnexionManager connexionManager = BusinessFactory.getConnexionManager();
				if(connexionManager.connect(form.getFieldValue("Login"),form.getFieldValue("Password"))){
					 if(form.getFieldValue("Login").equals("admin")){
						 new AdminFrame();
					 }else{
						 new FrameClient(BusinessFactory.getClientManager().find(form.getFieldValue("Login")));
					 }
					 dispose();
				}else{
					JOptionPane.showMessageDialog(null,"Mot de passe ou login est incorrect","Erreur",JOptionPane.ERROR_MESSAGE);
				}
			}
		});
	}
}
